#include "crisscross/universal_include.h"
#include "crisscross/core_filesystem.h"
#include "types.h"
#include "core.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

using namespace std;
using namespace CrissCross;
using namespace CrissCross::FileSystem;

#ifdef TARGET_OS_WINDOWS
#define popen _popen
#endif

extern dSymbol *T;
extern dList *nil;
extern "C" void load_module ();

class dFile : public dObject
{
 public:
  string filename;
  FILE *file;

  dFile ();
  dFile (const string filename, const char *open_mode="r");
  string toString ();
  ~dFile ();
};

dObject *dTypeFile (dList *args); // (filep object)
dObject *dOpenFile (dList *args); // (open file)
dObject *dReadFromFile (dList *args); // (read file 256)
dObject *dReadLineFromFile (dList *args); // (readln file)
dObject *dReadCharFromFile (dList *args); // (readc file)
dObject *dCloseFile (dList *args); // (close file)

dObject *dIsEOF (dList *args); // (is-eof file)

int isDirectory (string path);

dObject *dListDir (dList *args); // (list-dir "dir")
dObject *dMakeDir (dList *args); // (make-dir "dir")
dObject *dRemoveDir (dList *args); // (rm-dir "dir")

dObject *dTempFile (dList *args); // (temp-file)
dObject *dTempName (dList *args); // (temp-name)

//dObject *dFilePerm (dList *args); // (file-perms "file") => (r w x)
dObject *dIsDir (dList *args); // (is-dir "file") => T or nil
dObject *dIsFile (dList *args); // (is-file "file") => T or nil
dObject *dFileSize (dList *args); // (file-size "file") => 23409283 (in bytes!)
dObject *dFileExists (dList *args); // (file-exists "file") => T or nil
